﻿using Fluid;
using System.IO;
using ZYing.LiquidThemeEngine.Objects;

namespace ZYing.LiquidThemeEngine
{
    public class LiquidThemeObjectAccessor : IMemberAccessor
    {
        private LiquidThemeObjectAccessor()
        {
        }

        public static LiquidThemeObjectAccessor Instance { get; } = new();

        public object Get(object obj, string name, TemplateContext ctx)
        {
            string T(string name)
            {
                return ctx.Options.MemberAccessStrategy.MemberNameStrategy(ReflectionHelper.Get(name));
            }
            switch (obj)
            {
                case Template template:
                    if (name == T(nameof(Template.Name))) return template.Name;
                    if (name == T(nameof(Template.Directory))) return template.Directory;
                    break;
                case Paginate paginate:
                    if (name == T(nameof(Paginate.CurrentOffset))) return paginate.CurrentOffset;
                    if (name == T(nameof(Paginate.CurrentPage))) return paginate.CurrentPage;
                    if (name == T(nameof(Paginate.Items))) return paginate.Items;
                    if (name == T(nameof(Paginate.Parts))) return paginate.Parts;
                    if (name == T(nameof(Paginate.Previous))) return paginate.Previous;
                    if (name == T(nameof(Paginate.Next))) return paginate.Next;
                    if (name == T(nameof(Paginate.PageSize))) return paginate.PageSize;
                    if (name == T(nameof(Paginate.Pages))) return paginate.Pages;
                    break;
                case Paginate.PaginatePart part:
                    if (name == T(nameof(Paginate.PaginatePart.IsLink))) return part.IsLink;
                    if (name == T(nameof(Paginate.PaginatePart.Title))) return part.Title;
                    if (name == T(nameof(Paginate.PaginatePart.Url))) return part.Url;
                    break;
                case Engine engine:
                    if (name == T(nameof(Engine.Name))) return engine.Name;
                    if (name == T(nameof(Engine.Version))) return engine.Version;
                    break;
                case Section section:
                    if (name == T(nameof(Section.Id))) return section.Id;
                    if (name == T(nameof(Section.Settings))) return section.Settings;
                    if (name == T(nameof(Section.Blocks))) return section.Blocks;
                    break;
                case Block block:
                    if (name == T(nameof(Block.Id))) return block.Id;
                    if (name == T(nameof(Block.Settings))) return block.Settings;
                    if (name == T(nameof(Block.Type))) return block.Type;
                    break;
            }

            return null;
        }
    }
}